package org.e_gold.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.e_gold.util.Bet365;

@NamedNativeQueries({
	@NamedNativeQuery(
			name = "findByEventIdAndContractType",
			query = "select * from Contract c where c.contract_type = :contractType and c.event_id = :eventId",
			resultClass = Contract.class
			),
			@NamedNativeQuery(
					name = "findByEventIdAndContractTypeAndSection",
					query = "select * from Contract c where c.contract_type = :contractType and c.event_id = :eventId and c.section = :section",
					resultClass = Contract.class
					)	
})
@Entity
@Table(name = "contract")
public class Contract extends ValueObject implements Serializable, Bet365, Comparable<Contract> {

	private static final long serialVersionUID = 1L;

	public static final String CONTRACT_TYPE = "contractType";

	public static final String EVENT_ID = "event";

	@Id
	@GeneratedValue
	private Long id;

	@ManyToOne
	@JoinColumn(name = "event_id")
	private Event event;

	@Column(name = "contract_type")
	private String contractType;

	@Column(name = "bet_slip_url")
	private String betSlipUrl;

	@Column(name = "section")
	private String section;

	private Float rate;
	private Boolean qualify;

	public String getBetSlipUrl() {

		return BET_SLIP_BASE + betSlipUrl;
	}

	public void setBetSlipUrl(String betSlipUrl) {

		this.betSlipUrl = betSlipUrl;
	}

	public Boolean getQualify() {

		return qualify;
	}

	public void setQualify(Boolean qualify) {

		this.qualify = qualify;
	}

	public String getContractType() {

		return contractType;
	}

	public void setContractType(String contractType) {

		this.contractType = contractType;
	}

	public Event getEvent() {

		return event;
	}

	public void setEvent(Event event) {

		this.event = event;
	}

	public Long getId() {

		return id;
	}

	public void setId(Long id) {

		this.id = id;
	}

	public Float getRate() {

		return rate;
	}

	public void setRate(Float rate) {

		this.rate = rate;
	}

	public String getSection() {
		return section;
	}

	public void setSection(String section) {
		this.section = section;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((betSlipUrl == null) ? 0 : betSlipUrl.hashCode());
		result = prime * result
				+ ((contractType == null) ? 0 : contractType.hashCode());
		result = prime * result + ((event == null) ? 0 : event.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((qualify == null) ? 0 : qualify.hashCode());
		result = prime * result + ((rate == null) ? 0 : rate.hashCode());
		result = prime * result + ((section == null) ? 0 : section.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Contract)) {
			return false;
		}
		Contract other = (Contract) obj;
		if(other.getId().longValue() != id.longValue()){
			return false;
		}

		if (contractType == null) {
			if (other.contractType != null) {
				return false;
			}
		} else if (!contractType.equals(other.contractType)) {
			return false;
		}
		if (event == null) {
			if (other.event != null) {
				return false;
			}
		} else if (!event.equals(other.event)) {
			return false;
		}
		if (qualify == null) {
			if (other.qualify != null) {
				return false;
			}
		} else if (!qualify.equals(other.qualify)) {
			return false;
		}
		if (rate == null) {
			if (other.rate != null) {
				return false;
			}
		} else if (!rate.equals(other.rate)) {
			return false;
		}
		if (section == null) {
			if (other.section != null) {
				return false;
			}
		} else if (!section.equals(other.section)) {
			return false;
		}
		return true;
	}

	@Override
	public int compareTo(Contract o) {

		return o.getEvent().getPlayTime() - this.getEvent().getPlayTime();
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
}
